<?php
// +----------------------------------------------------------------------
// | layerIM + Workerman + ThinkPHP5 即时通讯
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\api\model\Chatlog as ChatlogModel;

class Chatlog extends Base
{
    //聊天记录
    public function index()
    {
        $id = input('id');
        $type = input('type');

        $this->assign([
            'id' => $id,
            'type' => $type
        ]);

        return $this->fetch();
    }

    //聊天记录详情
    public function detail()
    {
        $id = input('id');
        $type = input('type','friend');
        $uid = input('uid');

        $logModel=new ChatlogModel;
        if( 'friend' == $type ){
            $result = $logModel->where("(fromid={$uid}  or  toid={$uid}) and type='friend'")
                ->order('timeline desc')
                ->paginate(15, false, [
                    'query' => \request()->request()
                ]);

            if(!empty($result)){
                return $this->renderSuccess($result);
            }else{
                return $this->renderSuccess();
            }

        }else if('group' == $type){

            $result = $logModel->where("toid={$id} and type='group'")
                ->order('timeline desc')
                ->select();


            if(!empty($result)){
                return $this->renderSuccess(['data' => $result]);
            }else{
                return $this->renderSuccess();
            }

        }


    }
}